package com.wu.cloud.account.service.impl;

import bean.PeskResponseEntity;
import bean.vo.UserAddVO;
import bean.vo.UserQueryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.cloud.account.dao.UserAccountMapperDao;
import com.wu.cloud.account.entity.UserTableEntity;
import com.wu.cloud.account.service.AdminUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.ResponseUtil;

import java.util.List;

/**
 * @author xwu36
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<UserAccountMapperDao, UserTableEntity> implements AdminUserService {

    private final Logger log = LoggerFactory.getLogger(AdminUserServiceImpl.class);

    @Autowired
    UserAccountMapperDao userAccountMapperDao;

    @Override
    public PeskResponseEntity getUserListByAdmin(UserQueryVO userQueryVO) {
        QueryWrapper<UserTableEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserTableEntity> lambda = queryWrapper.lambda();
        if (userQueryVO.getUid() != null) {
            lambda.eq(UserTableEntity::getUserId, userQueryVO.getUid());
        }
        if (userQueryVO.getuName() != null) {
            lambda.like(UserTableEntity::getUserName, "%" + userQueryVO.getuName() + "%");
        }
        if (userQueryVO.getSex() != -1) {
            lambda.eq(UserTableEntity::getUserSex, userQueryVO.getSex());
        }
        if (userQueryVO.getState() != -1) {
            lambda.eq(UserTableEntity::getUserState, userQueryVO.getState());
        }
        List<UserTableEntity> userTableEntities = userAccountMapperDao.selectList(queryWrapper);
        return ResponseUtil.success(userTableEntities);
    }

    @Override
    public PeskResponseEntity addUserByAdmin(UserAddVO userAddVO) {
        UserTableEntity userTableEntity = new UserTableEntity();
        userTableEntity.setUserId(userAddVO.getUserId());
        userTableEntity.setUserName(userAddVO.getUserName());
        userTableEntity.setUserAge(userAddVO.getUserAge());
        userTableEntity.setUserSex(userAddVO.getUserSex());
        userTableEntity.setUserDesc(userAddVO.getUserDesc());
        userTableEntity.setWeiboLink(userAddVO.getWeiboLink());
        userTableEntity.setMailLink(userAddVO.getMailLink());
        int state = userAccountMapperDao.insert(userTableEntity);
        return state == 1 ? ResponseUtil.success() : ResponseUtil.fail();
    }

    @Override
    public PeskResponseEntity disableUserByAdmin(String uid) {
        UserTableEntity userTableEntity = new UserTableEntity();
        userTableEntity.setUserState(1);
        UpdateWrapper<UserTableEntity> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<UserTableEntity> lambda = updateWrapper.lambda();
        lambda.eq(UserTableEntity::getUserId, uid);
        int state = userAccountMapperDao.update(userTableEntity, lambda);
        return state == 1 ? ResponseUtil.success() : ResponseUtil.fail();
    }

    @Override
    public PeskResponseEntity updateUserByAdmin(UserAddVO userAddVO, String uid) {
        UserTableEntity userTableEntity = new UserTableEntity();
        userTableEntity.setUserName(userAddVO.getUserName());
        userTableEntity.setUserAge(userAddVO.getUserAge());
        userTableEntity.setUserSex(userAddVO.getUserSex());
        userTableEntity.setUserDesc(userAddVO.getUserDesc());
        userTableEntity.setWeiboLink(userAddVO.getWeiboLink());
        userTableEntity.setMailLink(userAddVO.getMailLink());
        UpdateWrapper<UserTableEntity> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<UserTableEntity> lambda = updateWrapper.lambda();
        lambda.eq(UserTableEntity::getUserId, uid);
        int state = userAccountMapperDao.update(userTableEntity, lambda);
        return state == 1 ? ResponseUtil.success() : ResponseUtil.fail();
    }
}
